---
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


- name: Install apt packages
  apt:
    pkg: "{{ item }}"
    state: "present"
    update_cache: yes
  register: install_packages
  until: install_packages|success
  retries: 5
  delay: 2
  with_items:
    - qemu
    - uuid-runtime
    - curl
    - kpartx
    - git
- name: Create Octavia tmp dir
  file:
    state: directory
    path: "/var/lib/octavia"
- name: Set Octavia tmp dir
  set_fact:
    bootstrap_host_octavia_tmp: "/var/lib/octavia"
- name: Install pip requirements
  pip:
    name: "{{ item }}"
    state: "present"
    extra_args: "-c {{ pip_install_upper_constraints_proto }}://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt?id={{ requirements_git_install_branch | regex_replace(' #.*$','') }}"
  register: install_packages
  until: install_packages|success
  retries: 5
  delay: 2
  with_items:
    - argparse
    - "Babel>=1.3"
    - dib-utils
    - PyYAML
    - diskimage-builder
- name: Clone Octavia
  git:
    repo: "https://git.openstack.org/openstack/octavia"
    dest: "{{ bootstrap_host_octavia_tmp }}/octavia"
    version: "{{ octavia_git_install_branch }}"
# Build Octavia amphora image
- name: Create amphora image
  shell: "./diskimage-create.sh -o {{ bootstrap_host_octavia_tmp }}/amphora-x64-haproxy.qcow2 -s3"
  args:
    chdir: "{{ bootstrap_host_octavia_tmp }}/octavia/diskimage-create"
    creates: "{{ bootstrap_host_octavia_tmp }}/amphora-x64-haproxy.qcow2"
  tags:
  - skip_ansible_lint
- name: Change permission
  file:
    path: "{{ bootstrap_host_octavia_tmp }}/octavia/bin/create_certificates.sh"
    mode: 0755
- name: Generate certs
  shell: "{{ bootstrap_host_octavia_tmp }}/octavia/bin/create_certificates.sh {{ bootstrap_host_octavia_tmp }}/certs {{ bootstrap_host_octavia_tmp }}/octavia/etc/certificates/openssl.cnf"
  args:
    creates: "{{ bootstrap_host_octavia_tmp }}/certs/ca_01.pem"
  tags:
  - skip_ansible_lint
- name: Fix certs/private directory access
  file:
    path: "{{ bootstrap_host_octavia_tmp }}/certs/private"
    mode: 0755
